<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <el-breadcrumb separator=">" style="height:50px;line-height:50px;">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ topCategory.name }}</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 轮播图 -->
      <el-carousel height="500px" autoplay>
        <el-carousel-item v-for="item in home.bannerList" :key="item.id">
          <img :src="item.imgUrl" />
        </el-carousel-item>
      </el-carousel>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in topCategory.children" :key="item.id">
            <router-link :to="`/category/sub/${item.id}`">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </router-link>
          </li>
        </ul>
      </div>
      <!-- 二级分类的商品列表 -->
      <div class="ref-goods" v-for="item in topCategory.children">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <xtx-more />
        </div>
        <div class="body">
          <GoodsItem
            v-for="goods in item.goods"
            :goods="goods"
            :key="goods.id"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup name="TopCategory">
import { ref, watch } from 'vue'
import { ApiRes } from '@/types/data'
import { TopCategory } from '@/types/category'
import { useRoute } from 'vue-router'
import request from '@/utils/request'
import { useStore } from '@/store'
import GoodsItem from './components/goods-item.vue'
const { home } = useStore()
const topCategory = ref({} as TopCategory)
const route = useRoute()

const getTopCategory = async () => {
  const res = await request.get<ApiRes<TopCategory>>(
    `category?id=${route.params.id}`
  )

  topCategory.value = res.data.result
}

watch(
  () => route.params.id,
  () => {
    // 滚动到顶部
    window.scroll({ top: 0 })
    // 判断只能加载一级分类
    if (route.fullPath === `/category/${route.params.id}`) {
      getTopCategory()
    }
  },
  {
    immediate: true
  }
)
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
